/**
 * @file geminai_mqtt_cache.h
 * @brief 队列上传信息到阿里云
 * @author zl
 */

#ifndef __GEMINAI_MQTT_CACHE__
#define __GEMINAI_MQTT_CACHE__

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "esp_log.h"
#include "geminai_log.h"
#include <time.h>
#include "aiot_shadow_api.h"
#include "aiot_dm_api.h"

#include "geminai_sdklink_connect.h"
#include <stdio.h>
#include <string.h>
#include "geminai_para.h"
#include "geminai_uart.h"
#include "geminai_hlw8032.h"
#include "geminai_adc.h"

#define MQTT_QUEUE_SIZE 10
#define MQTT_MSG_LEN 128

typedef enum
{
    AIOT_IAT_EVENT_NULL               = 0x00, ///< 空事件(无事件)
    AIOT_IAT_EVENT_CUSTOMER           = 0x01, ///< 客流事件
    AIOT_IAT_EVENT_SHOOT              = 0x02, ///< 拍摄事件
    AIOT_IAT_EVENT_SWITCH             = 0x03, ///< 开关事件
    AIOT_IAT_EVENT_DOOR               = 0x04, ///< 开关门事件
    AIOT_IAT_EVENT_VOICE              = 0x05, ///< 语音播报事件
    AIOT_IAT_EVENT_BOOTUP             = 0x06, ///< 开机事件
    AIOT_IAT_EVENT_LOCATION           = 0x07, ///< 定位事件
    AIOT_IAT_EVENT_DISPLAY_V2         = 0x08, ///< 陈列事件
    AIOT_IAT_EVENT_WARNING            = 0x09, ///< 告警事件
    AIOT_IAT_EVENT_COOR_SALES         = 0x0A, ///< 动销事件(端-云协同)(动销融合事件）
    AIOT_IAT_EVENT_SALE_TAKE_SNAPSHOT = 0x0B, ///< 动销拍摄事件
    AIOT_IAT_EVENT_CHECK_EVENT        = 0X0C, ///< 产检自检事件
} aiot_iat_event_enum;                        ///< 事件类型

/**
 * @brief 需要上报的参数
 *
 */
typedef enum
{
    PARA_SET_ALL                     = 16, //参数个数
    PARA_SET_TEMP_DIFF               = 0x00000001,  //回差温度
    PARA_SET_TEMP_DES                = 0x00000002,  //目标温度
    PARA_SET_DELAY_TIM               = 0x00000004,  //最小停机时间
    PARA_COOL_SWITCH                 = 0x00000008,  //制冷开关
    PARA_LIGHT_SWITCH                = 0x00000010,  //灯光开关
    PARA_LED_PWM                     = 0x00000020,  //灯光亮度
    PARA_EVAPORATOR_STATUS           = 0x00000040,  //蒸发风机状态
    PARA_COMPRESSOR_STATUS           = 0x00000080,  //压缩机状态
    PARA_VOLUME                      = 0x00000100,  //喇叭音量
    PARA_SHADOW                      = 0X00000200,  //影子模型
    PARA_COLD_NORMAL_STATE           = 0x00000400,  //正常状态
    PARA_COLD_DEFROST_STATE          = 0x00000800,  //除霜状态
    PARA_COLD_FROCE_STATE            = 0x00001000,  //强制制冷状态
    PARA_FRIG_RUN_MODE               = 0x00002000,  //运行模式
    PARA_OVERTEMP_ALARM               = 0x00004000,  //高温报警
    PARA_LOWTEMP_ALARM                = 0x00008000,  //低温报警
    // PARA_BOOT_SEND                   = 0x00000800,  //
} PARA_SET_PARA_TYPE;

typedef enum
{
    WARNING_CODE_LTE_ERROR                  = 60301, ///< 4G
    WARNING_CODE_MPU_ERROR                  = 60304, ///< 陀螺仪
    WARNING_CODE_CAM_V20_DISPLAY_DOWN_ERROR = 61005, ///< 元气2.0陈列摄像头
    WARNING_CODE_CAM_V20_SALE_ERROR         = 61006, ///< 元气2.0动销摄像头
    WARNING_CODE_LOGOLIGHT_ERROR            = 60310, ///< logo灯
    WARNING_CODE_POWER_ERROR                = 60314, ///< 电源计量
    WARNING_CODE_NTC_ERROR                  = 60315, ///< 温度传感器
    WARNING_CODE_COMPRESSOR_ERROR           = 60316, ///< 压缩机
    WARNING_CODE_BATTERY_ERROR              = 60321, ///< 电池
    WARNING_CODE_DOOR_CLOSE_TIMEOUT         = 60032, ///< 关门超时
    WARNING_CODE_OSS_REQUEST_FAIL           = 60033, ///< oss请求失败
    WARNING_CODE_OVERTEMP_ALARM             = 60034, ///高温报警
    WARNING_CODE_LOWTEMP_ALARM              = 60035, ///低温报警
} aiot_warning_code_enum;  

extern void *shadow_handler;
extern void *dm_handler;
extern QueueHandle_t mqtt_queue;

typedef enum {
    SHADOW,
    PHYSICAL
} Topic;

typedef struct {
    Topic topic;
    aiot_dm_msg_t* dmMessage;
    aiot_shadow_msg_t* shadowMessage; 
} MqttMessage;


//往主题中发送消息
void add_mqtt_message_to_queue(Topic topic, void* msg);
//初始化接收与发送mqtt消息
void mqtt_quere_init(void);
//发送事件
void aliyun_send_event_mqtt(aiot_iat_event_enum event, void *para);
//发送开机事件
void boot_event_send(void);
//视觉模块事件上报
int vision_event_report(char* event, void *para);
//属性发送任务
void attribute_send_task(void* arg);
//定时上报消息
void regularly_report_task(void* arg);
//告警事件发送
void warning_event_send(aiot_warning_code_enum code, int state);
//掉电定位事件发送
void send_shutdown_base_station_event(void);
//发送影子模型
void send_shadow_update_event(void);
//停止属性发送任务
void attribute_send_task_stop(void);
//启动属性发送任务
void attribute_send_task_start(void);
//服务调用回复
void service_invoke_reply(char *service_id, char *rrpc_id, uint64_t msg_id, char *data);
//发送定位信息
void send_location_info(void);
#endif